package com.eascs.p2p.common.wallet.utils;

import java.math.BigDecimal;

import com.eascs.p2p.common.entity.InvestObjectEntity;

public class BigDecimalUtils {

	public static final BigDecimal NUMBER_YEAR = new BigDecimal(12);

	public static final BigDecimal NUMBER_DAY = new BigDecimal(30);

	public static final BigDecimal NUMBER_HUNDRED = new BigDecimal(100);

	public static BigDecimal convertNull(BigDecimal value) {
		if (value == null) {
			return new BigDecimal(0);
		}
		return value;
	}

	/**
	 * @param 投资本金
	 * @param 年化率
	 * @param 期限天数
	 * @param 期限类型：0-天 1-月 
	 *           预期收益=投资本金*（年化收益率/12/30）*期限天数/100
	 */
	public static BigDecimal incomeHandle(BigDecimal investAmount, BigDecimal rateYield, BigDecimal deadline,
			int deadlineType) {
		if (deadlineType == InvestObjectEntity.DEADLINE_TYPE_YEAR) {
			deadline = deadline.multiply(BigDecimalUtils.NUMBER_DAY);
		}
		/**
		 * 防止进度问题，先乘法后除法
		 * 预期收益=(投资本金*年化收益率*期限天数) / (12*30*100)
		 * */
		//除数
		BigDecimal divisor= BigDecimalUtils.NUMBER_YEAR.multiply(BigDecimalUtils.NUMBER_DAY).multiply(BigDecimalUtils.NUMBER_HUNDRED);
		//被除数
		BigDecimal dividend = investAmount.multiply(rateYield).multiply(deadline);
		//收益
		BigDecimal income = dividend.divide(divisor,2,BigDecimal.ROUND_HALF_UP);
		//income = investAmount.add(income);
		return income;
	}
}
